/*
 * @Author       : Deng
 * @Date         : 2024-04-26 14:25:10
 * @LastEditors  : Joel
 * @LastEditTime : 2024-05-09 12:02:14
 * @FilePath     : \src\main.c
 * @Description  :
 * Copyright 2024 YiJiaLink, All Rights Reserved.
 * 2024-04-26 14:25:10
 */
#include "rgb.h"

/* 自动clean,make,flash请在终端输入执行：make 指令*/

init_rgb_pwm set_rgb_pwm, *p_pwm;

void Delay10ms(void); //@33.1776MHz
void Delay1ms(void);
/*******************************************************************************
 * 函 数 名       : time0_init
 * 函数功能		 : 定时器0中断配置函数，通过设置TH和TL即可确定定时时间
 * 输    入       : 无
 * 输    出    	 : 无
 *******************************************************************************/
void time0_init(void)
{
	AUXR |= 0x80; // 定时器时钟1T模式
	TMOD &= 0xF0; // 设置定时器模式
	TL0 = 0xFF;	  // 设置定时初始值
	TH0 = 0xFF;	  // 设置定时初始值
	TF0 = 0;	  // 清除TF0标志
	ET0 = 1;	  // 打开定时器0中断允许
	EA = 1;		  // 打开总中断
	TR0 = 1;	  // 打开定时器
	return;
}

/*******************************************************************************
 * 函 数 名       : main
 * 函数功能		 : 主函数
 * 输    入       : 无
 * 输    出    	 : 无
 *******************************************************************************/
int main(void)
{
	time0_init(); // 定时器0中断配置
	p_pwm = &set_rgb_pwm;
	p_pwm->white_duty = PWM_CYCLE;
	for (;;)
	{
		/* code */
		switch (p_pwm->brightness_index)
		{
		case RED /* constant-expression */:
			/* code */
			if (PWM_CYCLE == p_pwm->red_duty)
				p_pwm->brightness_index++;
			else
			{
				++p_pwm->red_duty;
				--p_pwm->white_duty;
			}
			break;
		case GREEN /* constant-expression */:
			/* code */
			if (PWM_CYCLE == p_pwm->green_duty)
				p_pwm->brightness_index++;
			else
			{
				++p_pwm->green_duty;
				--p_pwm->red_duty;
			}

			break;
		case BLUE /* constant-expression */:
			/* code */
			if (PWM_CYCLE == p_pwm->blue_duty)
				p_pwm->brightness_index++;
			else
			{
				++p_pwm->blue_duty;
				--p_pwm->green_duty;
			}

			break;
		case WHITE /* constant-expression */:
			/* code */
			if ((PWM_CYCLE) == p_pwm->white_duty)
				p_pwm->brightness_index++;
			else
			{
				++p_pwm->white_duty;
				--p_pwm->blue_duty;
			}
			break;
		default:
			p_pwm->white_duty = PWM_CYCLE;
			p_pwm->red_duty = 0;
			p_pwm->green_duty = 0;
			p_pwm->blue_duty = 0;
			p_pwm->brightness_index = RED;
			break;
		}
		Delay10ms();
		
	}

	return 0;
}
/**
 * @func:
 * @description: 软件阻塞延迟
 * @return {*}
 * @example:
 */
void Delay10ms(void) //@33.1776MHz
{
	unsigned char i, j;

	i = 20;
	j = 49;
	do
	{
		while (--j)
			;
	} while (--i);
	return;
}
/**
 * @func:
 * @description: 软件阻塞延迟
 * @return {*}
 * @example:
 */
void Delay1ms(void) //@33.1776MHz
{
	unsigned char i, j;

	i = 3;
	j = 45;
	do
	{
		while (--j)
			;
	} while (--i);
}
/**
 * @func:
 * @description: Timer0中断入口
 * @return {*}
 * @example:
 */
void Timer0(void) __interrupt(1) // 定时器0中断函数
{
/* 红色不带死区PWM */
#ifndef DEAD_ZONE
	if (p_pwm->red_duty >= PWM_CYCLE)
	{
		__asm__("setb 0x90\n");

		p_pwm->index_red = 0;
	}
	else if (p_pwm->red_duty == 0)
	{
		__asm__("clr 0x90\n");
		p_pwm->index_red = 0;
	}
	else
#endif
	if (++p_pwm->index_red >= PWM_CYCLE)
	{

		__asm__("setb 0x90\n");
		p_pwm->index_red = 0;
	}
	else if (p_pwm->index_red >= p_pwm->red_duty)
	{
		__asm__("clr 0x90\n");
	}

/* 绿色不带死区PWM */
#ifndef DEAD_ZONE
	if (p_pwm->green_duty >= PWM_CYCLE)
	{
		__asm__("setb 0x91\n");

		p_pwm->index_green = 0;
	}
	else if (p_pwm->green_duty == 0)
	{
		__asm__("clr 0x91\n");
		p_pwm->index_green = 0;
	}
	else
#endif
		if (++p_pwm->index_green >= PWM_CYCLE)
	{

		__asm__("setb 0x91\n");
		p_pwm->index_green = 0;
	}
	else if (p_pwm->index_green >= p_pwm->green_duty)
	{
		__asm__("clr 0x91\n");
	}

/* 蓝色不带死区PWM */
#ifndef DEAD_ZONE
	if (p_pwm->blue_duty >= PWM_CYCLE)
	{
		__asm__("setb 0x92\n");

		p_pwm->index_blue = 0;
	}
	else if (p_pwm->blue_duty == 0)
	{
		__asm__("clr 0x92\n");
		p_pwm->index_blue = 0;
	}
	else
#endif
	if (++p_pwm->index_blue >= PWM_CYCLE)
	{

		__asm__("setb 0x92\n");
		p_pwm->index_blue = 0;
	}
	else if (p_pwm->index_blue >= p_pwm->blue_duty)
	{
		__asm__("clr 0x92\n");
	}

	/* 白色不带死区PWM */
#ifndef DEAD_ZONE
	if (p_pwm->white_duty >= PWM_CYCLE)
	{
		__asm__("setb 0x93\n");

		p_pwm->index_white = 0;
	}
	else if (p_pwm->white_duty == 0)
	{
		__asm__("clr 0x93\n");
		p_pwm->index_white = 0;
	}
	else
#endif
	if (++p_pwm->index_white >= PWM_CYCLE)
	{

		__asm__("setb 0x93\n");
		p_pwm->index_white = 0;
	}
	else if (p_pwm->index_white >= p_pwm->white_duty)
	{
		__asm__("clr 0x93\n");
	}
	return;
	/* 调试用 */

}